<?php

namespace App\Http\Controllers;


use App\Eloquent\Message;
use App\Eloquent\User;
use App\Eloquent\UserMessage;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class MessageController extends Controller
{

    const SYS_MESSAGE_TYPE = 'message_type';

    /**
     * 站内信
     * 
     * @param  String|string $message_type [description]
     * @return [type]                      [description]
     */
    public function index(String $message_type = '')
    {
        $user = User::user();
        // $menu = 3;
        // $user_id = User::user()->id;

        if (empty($message_type)) {
            $user_messages = Message::select('messages.id', 'messages.title', 'messages.published_at', 'messages.content', 'b.readed')
                ->leftJoin('user_messages as b', 'messages.id', '=', 'b.message_id')
                ->where('b.user_id', $user->id)
                ->where('published_at', '<=', date('Y-m-d H:i:s'))
                ->orderBy('readed', 'asc')
                ->orderBy('published_at', 'desc')
                ->paginate(8);
        } else {
            $user_messages = Message::select('messages.id', 'messages.title', 'messages.published_at', 'messages.content', 'b.readed')
                ->leftJoin('user_messages as b', 'messages.id', '=', 'b.message_id')
                ->where('b.user_id', $user->id)
                ->where('messages.type', $message_type)
                ->where('published_at', '<=', date('Y-m-d H:i:s'))
                ->orderBy('readed', 'asc')
                ->orderBy('published_at', 'desc')
                ->paginate(8);
        }

        //查询所有消息类型
        $message_types = DB::select('select a.code,a.name from codes as a WHERE a.type_id=\'' . self::SYS_MESSAGE_TYPE . '\' order by sort desc');


        $data = [
            'user_messages' => $user_messages,
            'message_types' => $message_types,
            'query_message_type' => $message_type,
        ];

        return view('web.messages.user_messages')->with($data);
    }

    public function delete_msg(Request $request)
    {
        //获取删除的消息id
        $msg_id = $request->get('id');


        $user_id = User::user()->id;

        //执行删除动作
        UserMessage::where('user_id', $user_id)->where('message_id', $msg_id)->delete();

        return 'success';
    }

    public function read_msg(Request $request)
    {
        //获取要读取的消息id
        $msg_id = $request->get('id');


        $user_id = User::user()->id;
        //将消息标记为已读
        UserMessage::where('user_id', $user_id)->where('message_id', $msg_id)->update(['readed' => 1]);

        return 'success';
    }

    public function read_all_msg(Request $request)
    {
        //获取要置为已读的消息类型
        $msg_type = $request->get('message_type');

        $user_id = User::user()->id;

        if (empty($msg_type)) {
            //将用户全部消息标记为已读
            UserMessage::where('user_messages.user_id', $user_id)->update(['readed' => 1]);
        } else {
            //将某个指定消息分类消息置为已读
            UserMessage::leftJoin('messages as a', 'user_messages.message_id', '=', 'a.id')
                ->where('a.type', $msg_type)
                ->where('user_messages.user_id', $user_id)->update(['readed' => 1]);
        }
        return 'success';
    }

    public function clear_all_msg(Request $request)
    {
        //获取要置为已读的消息类型
        $msg_type = $request->get('message_type');

        $user_id = User::user()->id;

        if (empty($msg_type)) {
            //删除用户全部消息
            UserMessage::where('user_messages.user_id', $user_id)->delete();
        } else {
            //将某个指定消息分类消息删除
            UserMessage::leftJoin('messages as a', 'user_messages.message_id', '=', 'a.id')
                ->where('a.type', $msg_type)
                ->where('user_messages.user_id', $user_id)
                ->delete();
        }

        return 'success';
    }
}